package com.example.Coupons.service.impl.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.Coupons.entity.Coupon;
import com.example.Coupons.mapper.CouponMapper;
import com.example.Coupons.service.impl.CouponService;
import com.example.Utility.publicUtils;
import com.example.entity.OrderMessage;
import com.example.entity.lordOrder;
import com.example.service.OrderMessageSrvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Service
public class CouponImpl extends ServiceImpl<CouponMapper, Coupon> implements CouponService {
    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private OrderMessageSrvice orderMessageSrvice;

    @Autowired
    private CouponService couponService;

    @Override
    public List<Coupon> serviceList(Coupon coupon) {
        List<Coupon> list = couponMapper.selectList(new QueryWrapper<Coupon>().like("openid", coupon.getOpenid()));
        return list;
    }

    @Override
    public List<Coupon> getCouponList(List<OrderMessage> orderMessages) throws Exception {
        List<Long> id = new ArrayList<>();
        for (OrderMessage orderMessage : orderMessages) {
            id.add(orderMessage.getGoodsId());
        }
        LambdaQueryWrapper<Coupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Coupon::getAssignId, id)
                .gt(Coupon::getOverTime, publicUtils.getTimes("."))
                .eq(Coupon::getState, 0)
                .eq(Coupon::getOpenid, orderMessages.get(0).getOpenid());
        List<Coupon> coupons = couponService.list(queryWrapper);
        if (!StringUtils.isEmpty(coupons)) {
            return coupons;
        }
        return null;
    }


    /**
     * 处理优惠券信息
     *
     * @param
     * @return
     */
    @Override
    @Transactional
    public BigDecimal dealCoupon(lordOrder lordOrder) {
        //        第一步获取当前优惠券id 获取信息
        LambdaQueryWrapper<Coupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Coupon::getId, lordOrder.getDiscountsId());
        Coupon coupon = couponService.getOne(queryWrapper);
        if (StringUtils.isEmpty(coupon)) {
            return new BigDecimal("0");
        } else {
            coupon.setState(1);
            couponService.updateById(coupon);
        }
        LambdaQueryWrapper<OrderMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderMessage::getGoodsId, coupon.getAssignId())
                .eq(OrderMessage::getMId, lordOrder.getId());
        OrderMessage orderMessage = orderMessageSrvice.getOne(wrapper);
        orderMessage.setUnitPrice(orderMessage.getUnitPrice().subtract(new BigDecimal(coupon.getMoney())));
        orderMessage.setDiscountsId(coupon.getId());
        if (orderMessageSrvice.updateById(orderMessage)) {
            return new BigDecimal(coupon.getMoney());
        }
//        如果不存在 这返回0 表示没有使用优惠券
        return new BigDecimal("0");
    }
}
